﻿(function () {
    function cssifyLength(length) {
        var numberRegex = /^\d+(?:\.\d+)?$/;
        if (numberRegex.test(length)) { return length + 'px'; }
        return length;
    }
    function getFloat(s) {
        var r = new RegExp(/float\:[^\w]*(?:left|right)[^;]*/);
        var m = r.exec(s);
        var o = '';
        if (m != null && m[0].replace(' ', '') != 'none') o = m[0].replace(' ', '');
        return o.substring(o.indexOf(':') + 1);
    }
    function isModuleplaceholder(c) {
        var r = new RegExp(/[^\w]*(moduleplaceholder)[^\w]*/);
        var m = r.exec(c);
        if (m != null) { return true; }
        else { return false; }
    }
    CKEDITOR.plugins.add('moduleplaceholder', {
        init: function (c) {
            var b = 'moduleplaceholder';
            CKEDITOR.dialog.add(b, this.path + 'dialogs/moduleplaceholder.js');
            c.addCommand(b, new CKEDITOR.dialogCommand(b));
            c.ui.addButton('Module Placeholder', { label: 'Module Placeholder', command: b });
            c.addCss('img.moduleplaceholder{width:150px;height:100px;border:dashed 1px black;background-color:#F8F5E1;text-align:center;background-image:url(/cms/images/Puzzle_BKG.png);background-position:100% 100%;background-repeat:no-repeat;color:#000;font-weight:bold;font-size:10pt;}');
            c.on('doubleclick', function (evt) {
                var element = evt.data.element;
                if (element.is('img') && element.getAttribute('_cke_real_element_type') == 'moduleplaceholder') evt.data.dialog = 'moduleplaceholder';
            });
        },
        afterInit: function (editor) {
            var dataProcessor = editor.dataProcessor;
            var dataFilter = dataProcessor && dataProcessor.dataFilter;
            if (dataFilter) {
                dataFilter.addRules({
                    elements: {
                        'div': function (element) {
                            var attributes = element.attributes;
                            if (isModuleplaceholder(attributes['class'])) {
                                var fakeElement = editor.createFakeParserElement(element, 'moduleplaceholder', 'moduleplaceholder', true),
                                fakeStyle = fakeElement.attributes['style'] || '';
                                var width = element.attributes.width,
			                    height = element.attributes.height;
                                if (typeof width != 'undefined') fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength(width) + ';';
                                if (typeof height != 'undefined') fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength(height) + ';';
                                fakeElement.attributes.align = getFloat(element.attributes['style']);
                                return fakeElement;
                            }
                            else { return element; }
                        }
                    }
                }, 5);
            }
        },
        requires: ['fakeobjects']
    });
})();